//
//  MainViewController.swift
//  基于Swift的微博设计与实现
//
//  Created by 张源海 on 15/11/22.
//  Copyright © 2015年 张源海. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    // MARK: - 监听方法
    /// 点击撰写按钮
    /// 如果`单纯`使用 `private` 运行循环将无法正确发送消息，导致崩溃
    /// 如果使用 @objc 修饰符号，可以保证运行循环能够发送此消息，即使函数被标记为 private
    
    @objc private func clickComposedButton() {
//        print("点我了")
        
        
    }
    // MARK: - 视图生命周期函数
    override func viewDidLoad() {
        
        super.viewDidLoad()
        // 添加控制器，并不会创建 tabBar 中的按钮！
        // 懒加载是无处不在的，所有控件都是延迟创建的！
        addChildViewControllers()
        
        
//        print(tabBar.subviews)
        setupComposedButton()
       
        }
    
    override func viewWillDisappear(animated: Bool) {
        
        super.viewWillAppear(animated)
        
       // super.viewWillAppear(animated)
        //setupComposedButton()
        // 将撰写按钮弄到最前面
        tabBar.bringSubviewToFront(composedButton)
        
    }
    
    
    
    // Mark: - 懒加载控件
    private lazy var composedButton: UIButton = UIButton(
        imageName: "tabbar_compose_icon_add",
        backImageName: "tabbar_compose_button")
}
//    // MARK: - 懒加载控件
//    private lazy var composedButton:UIButton = {
//        // 自定义样式 的 按钮
//        let button = UIButton()
//        
////        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
////        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
////        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
////        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
////        
////        // 会根据背景图片的大小调整尺寸 
////        
////        button.sizeToFit()
//    
//        return button
//    
//    }()
//}
    
    // MARK: - 设置界面
extension MainViewController{
        
        ///  设置撰写按钮 
        private func setupComposedButton() {
            
            // 添加按钮
            tabBar.addSubview(composedButton)
            // 调整按钮 
            let count = childViewControllers.count
            
            
            
            
            //  让按钮宽一点点，能够解决手指触摸的容错问题
            
            let w = tabBar.bounds.width / CGFloat(count) - 1
            
            composedButton.frame = CGRectInset(tabBar.bounds,  2 * w, 0)
            
            composedButton.addTarget(self, action: "clickComposedButton", forControlEvents: .TouchUpInside)
            
//            let w = tabBar.bounds.width / CGFloat(count) -1
//            
//            composedButton.frame = CGRectInset(tabBar.bounds, w * 2, 0)
//            
        }
    
    
    /// 添加所有的控制器
    private func addChildViewControllers() {
        tabBar.tintColor = UIColor.orangeColor()
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(ProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
        
        
    }
    
    
    
 /// 添加控制器
    
    
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        
        // 设置标题 
        vc.title =  title
        
        vc.tabBarItem.image = UIImage(named:imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }

    
    

  }

